﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

// Character.cs
// Contains information for specific playable characters, used in both Battle 
// and Area situations.
namespace RTS_RPG
{
    public class Character
    {
        public static short FIRST_LEVEL_UP_XP_AMOUNT = 10;

        // Character Fields
        public String name;
        public Vector2 hp;       // [X] = current hp, [Y] = max hp
        public Vector2 mp;       // [X] = current mp, [Y] = max mp
        public Vector2 xp;       // [X] = current xp, [Y] = next level
        public int level;
        public int weapon_range;
        public Vector4 stats;    // [X] = pow, [Y] = def, [Z] = spd, [W] = int
        public int freePoints;

        public Texture2D avatar;            //textures used in battle (and maybe scenes)
        public Texture2D representation;
        public Texture2D attacking;
        public AI routine;
        public List<Ability> abilities;
        public List<Ability> learnable;

        // Constructor that can be used when either loading a saved game or creating a new character.
        public Character(String nm, Vector2 _hp, Vector2 _mp, Vector2 _xp, int _level, 
            int rng, Vector4 sts, int fp, Texture2D av, 
            Texture2D rep, Texture2D atking, AI r, List<Ability> ab, List<Ability> lrn)
        {
            name = nm;
            hp = _hp;
            mp = _mp;
            xp = _xp;
            if (xp.X == Program.NaN) xp = new Vector2(0, FIRST_LEVEL_UP_XP_AMOUNT);
            level = _level;
            weapon_range = rng;
            stats = sts;
            freePoints = fp;
            avatar = av;
            representation = rep;
            attacking = atking;
            routine = r;
            abilities = ab;
            learnable = lrn;
        }

        public Boolean check_level_up()
        {
            if (xp.X >= xp.Y)
            {
                float temp = xp.X - xp.Y;
                xp.X = temp;
                xp.Y *= 2;
                level_up();
                check_level_up();   // check for another level up in case we just got a ton of exp.
                return true;
            }
            return false;
        }

        private void level_up()
        {
            ++level;
            freePoints += 3;
            hp.X += level;
            hp.Y += level;
            mp.X += level;
            mp.Y += level;
        }

        // For drawing in areas and on the overworld (and during scenes/events?)
        public void draw(SpriteBatch spBatch, Vector2 location)
        {
            spBatch.Draw(representation, 
                new Vector2(location.X - representation.Width / 2, location.Y - representation.Height / 2), 
                Color.White);
        }
    }
}
